package io.zt.dangyuan;

import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bumptech.glide.Glide;

import org.xutils.ex.HttpException;

import java.util.HashMap;

import de.hdodenhof.circleimageview.CircleImageView;
import io.zt.dangyuan.util.Cons;

/**
 * Created by Administrator on 2015/11/11.
 */


public class PersonalActivity2 extends BaseActivity {
    private CircleImageView people_head;//头像
    private TextView tv_name;//名字
    private TextView tv_sex;//性别
    private TextView tv_group;//分组
    private TextView tv_unit;//单位
    private TextView tv_post;//职务
    private TextView tv_address;//地址
    private TextView tv_phone;//手机
    private TextView tv_email;//email
    private TextView tv_tel;//电话
    private TextView tv_weixin;//微信
    private Button button;//修改
    private TextView tv_birthday;//生日
    private ImageView come_back;

    private int userid;
    private String groupname;
    private String headUrl;
    private String url = "groupInfo.json?userid=";
    private String tel;
    private String phone;
    private String headUri = Cons.BASE_URL;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_people_info_personal2);
        initView();
        final GlobalDataApplication app = (GlobalDataApplication) getApplication();
        userid = app.getUserModel().getUserid();
        Intent intent = getIntent();
        groupname = intent.getStringExtra("groupname");
        getPeopleInfo();
        bindEvents();
    }

    public void initView() {
        button=(Button)findViewById(R.id.button);
        come_back=(ImageView)findViewById(R.id.come_back);
        people_head = (CircleImageView) findViewById(R.id.people_head);
        tv_name = (TextView) findViewById(R.id.tv_name);
        tv_sex = (TextView) findViewById(R.id.tv_sex);
        tv_group = (TextView) findViewById(R.id.tv_group);
        tv_unit = (TextView) findViewById(R.id.tv_unit);
        tv_post = (TextView) findViewById(R.id.tv_post);
        tv_address = (TextView) findViewById(R.id.tv_address);
        tv_phone = (TextView) findViewById(R.id.tv_phone);
        tv_email = (TextView) findViewById(R.id.tv_email);
        tv_tel = (TextView) findViewById(R.id.tv_tel);
        tv_birthday=(TextView)findViewById(R.id.tv_birthday);
        tv_weixin = (TextView) findViewById(R.id.tv_weixin);

//        bt_call_up = (Button)findViewById(R.id.bt_call_up);
//        bt_sent_message = (Button)findViewById(R.id.bt_sent_message);

//        GradientDrawable drawable = new GradientDrawable();
//        drawable.setShape(GradientDrawable.RECTANGLE);
//        drawable.setStroke(2, Color.BLACK);
//        drawable.setCornerRadius(30.0f);
//        drawable.setColor(Color.WHITE);
//        bt_call_up.setBackgroundDrawable(drawable);
//        bt_sent_message.setBackgroundDrawable(drawable);
    }
    private void bindEvents(){
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(PersonalActivity2.this, PersonalActivity.class));
            }
        });
        come_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }
    public void getPeopleInfo() {
        if (!isConnect(this)) {
            Toast.makeText(this, "网络异常！", Toast.LENGTH_LONG).show();
        }
        //final ProgressDialog pd = new ProgressDialog(this);
        //pd.setProgress(ProgressDialog.STYLE_SPINNER);
        //pd.setMessage("正在加载...");
        //pd.show();
        WebUtils.sendPost(PersonalActivity2.this, url+userid ,new HashMap<String,Object>(), new WebUtils.OnWebListenr() {
            @Override
            public void onSuccess(boolean isSuccess, String msg, String data) {
                String result = data;
                try {
                    org.json.JSONObject object = new org.json.JSONObject(result);
                    org.json.JSONObject json = object.getJSONObject("data");
                    tv_name.setText("姓名:"+json.getString("realname"));
                    String sex = json.getString("sex");
                    if (sex.equals("1")) {
                        tv_sex.setText("性别：男");
                    } else if (sex.equals("0")) {
                        tv_sex.setText("性别：女");
                    } else {
                        tv_sex.setText("性别：保密");
                    }
                    tv_birthday.setText("生日：" +json.getString("birthday"));
                    Log.e("tag........", json.getString("birthday"));
                    tv_group.setText("分组：" + groupname);
                    tv_unit.setText("单位：" + json.getString("workunit"));
                    //Log.e("tag........", json.getString("workunit"));
                    tv_address.setText("地址：" + json.getString("address"));
                    Log.e("tag........", json.getString("address"));
                    tv_post.setText("职位：" + json.getString("position"));
                    Log.e("tag........", json.getString("position"));
                    phone = json.getString("mobile");
                    tv_phone.setText("手机：" + phone);
                    Log.e("tag........", phone);
                    tv_email.setText("Email：" + json.getString("email"));
                    Log.e("tag........", json.getString("email"));
                    tel = json.getString("phone");
                    tv_tel.setText("电话：" + tel);
                    tv_weixin.setText("微信：" + json.getString("weixin"));
                    headUrl = json.getString("userimg");
                    if (headUrl != null && !headUrl.equals("")) {
                        Glide.with(PersonalActivity2.this).load(headUri + headUrl).placeholder(R.drawable.ic_laucher).into(people_head);
                    }
                    //pd.dismiss();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailed(HttpException error, String msg) {
                Toast.makeText(PersonalActivity2.this, "数据请求失败" + msg, Toast.LENGTH_LONG).show();
            }
        });
    }

    /**
     * 判断手机是否联网
     */
    public static boolean isConnect(Context context) {

        //当网络连接管理器
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        //获取当前设备的所有联网方式
        NetworkInfo info = manager.getActiveNetworkInfo();
        if(info!=null && info.isAvailable()){
            return true;
        }
        return  false;
//        int flag = 0;
//        for (int i = 0; i < info.length; i++) {
//            //显示网络连接是否存在
//            if (info[i].isConnected()) {
//                flag++;
//            }
//        }
//        return flag > 0;
    }




}



